package net.zoneland.knowledge.service.impl.search;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.google.common.collect.Lists;

import net.zoneland.knowledge.model.EsQuestion;
import net.zoneland.knowledge.model.QaQueryDto;
import net.zoneland.knowledge.model.QaResponse;

/**
 * .
 *
 * @author: bcc
 * 2021/5/26
 */
public class SearchDto implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否停止搜索.
     * 用于控制链是否执行.
     */
    private boolean stopSearch;

    /**
     * 是否停止处理.
     * 用于控制链是否执行.
     */
    private boolean stopDeal;

    /**
     * 查询参数.
     */
    private QaQueryDto qaQueryDto;

    /**
     * 返回对象.
     */
    private QaResponse qaResponse;

    /**
     * answerId.
     */
    private String answerId;

    /**
     * 查询出的题库问题.
     */
    private List<EsQuestion> esQuestions;

    /**
     * 用于保存问答记录的,防止解析excel.
     */
    private String leve1Title;

    /**
     * 搜索出错
     */
    private boolean errorFlag;
    
    /**
     * .
     *
     * @return serialVersionUID
     */
    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    /**
     * .
     *
     * @return stopSearch
     */
    public boolean isStopSearch() {
        return stopSearch;
    }

    /**
     * .
     *
     * @param stopSearch to set
     */
    public void setStopSearch(final boolean stopSearch) {
        this.stopSearch = stopSearch;
    }

    /**
     * .
     *
     * @return stopDeal
     */
    public boolean isStopDeal() {
        return stopDeal;
    }

    /**
     * .
     *
     * @param stopDeal to set
     */
    public void setStopDeal(final boolean stopDeal) {
        this.stopDeal = stopDeal;
    }

    /**
     * .
     *
     * @return qaQueryDto
     */
    public QaQueryDto getQaQueryDto() {
        return qaQueryDto;
    }

    /**
     * .
     *
     * @param qaQueryDto to set
     */
    public void setQaQueryDto(final QaQueryDto qaQueryDto) {
        this.qaQueryDto = qaQueryDto;
    }

    /**
     * .
     *
     * @return qaResponse
     */
    public QaResponse getQaResponse() {
        return qaResponse;
    }

    /**
     * .
     *
     * @param qaResponse to set
     */
    public void setQaResponse(final QaResponse qaResponse) {
        this.qaResponse = qaResponse;
    }

    /**
     * .
     *
     * @return answerId
     */
    public String getAnswerId() {
        return answerId;
    }

    /**
     * .
     *
     * @param answerId to set
     */
    public void setAnswerId(final String answerId) {
        this.answerId = answerId;
    }

    /**
     * .
     */
    public void generateAnserId() {
        if (null == answerId) {
            answerId = UUID.randomUUID().toString();
        }
    }

    /**
     * .
     *
     * @return esQuestions
     */
    public List<EsQuestion> getEsQuestions() {
        return Optional.ofNullable(esQuestions).map(it -> Lists.newArrayList(it.iterator())).orElse(null);
    }

    /**
     * .
     *
     * @param esQuestions to set
     */
    public void setEsQuestions(final List<EsQuestion> esQuestions) {
        this.esQuestions = Optional.ofNullable(esQuestions).map(it -> Lists.newArrayList(it.iterator())).orElse(null);
    }

    /**
     * 添加答案.
     *
     * @param esQuestions to set
     */
    public void addEsQuestions(final List<EsQuestion> esQuestions) {
        List<EsQuestion> tmpEsQuestions = Optional.ofNullable(this.esQuestions)
            .map(it -> Lists.newArrayList(it.iterator())).orElse(Lists.newArrayList());

        final Set<String> existsId = tmpEsQuestions.stream()
            .map(EsQuestion::getId)
            .collect(Collectors.toSet());
        
        Optional.ofNullable(esQuestions).ifPresent(it -> {
            final List<EsQuestion> listNotContain = it.stream()
                .filter(x -> !existsId.contains(x.getId()))
                .collect(Collectors.toList());
            tmpEsQuestions.addAll(listNotContain);});
        this.esQuestions = tmpEsQuestions;
    }

    /**
     * .
     *
     * @return leve1Title
     */
    public String getLeve1Title() {
        return leve1Title;
    }

    /**
     * .
     *
     * @param leve1Title to set
     */
    public void setLeve1Title(final String leve1Title) {
        this.leve1Title = leve1Title;
    }

    /**
     * .
     *
     * @return errorFlag
     */
    public boolean isErrorFlag() {

        return errorFlag;
    }


    /**
     * .
     *
     * @param errorFlag to set
     */
    public void setErrorFlag(final boolean errorFlag) {

        this.errorFlag = errorFlag;
    }

    @Override
    public String toString() {

        final StringBuilder sb = new StringBuilder("SearchDto{");
        sb.append("stopSearch=")
            .append(stopSearch);
        sb.append(", stopDeal=")
            .append(stopDeal);
        sb.append(", qaQueryDto=")
            .append(qaQueryDto);
        sb.append(", qaResponse=")
            .append(qaResponse);
        sb.append(", answerId='")
            .append(answerId)
            .append('\'');
        sb.append(", esQuestions=")
            .append(esQuestions);
        sb.append(", leve1Title='")
            .append(leve1Title)
            .append('\'');
        sb.append(", errorFlag=")
            .append(errorFlag);
        sb.append('}');
        return sb.toString();
    }

}
